/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.plugins;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.grayrabbit.cms.action.common.BaseAction;
import org.grayrabbit.cms.constant.ConstantWebApp;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.constant.ConstantEnum.Module;
import org.grayrabbit.cms.constant.ConstantEnum.OperAct;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Category;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.entity.Dictionary;
import org.grayrabbit.cms.entity.Site;
import org.grayrabbit.cms.entity.Vote;
import org.grayrabbit.cms.entity.VoteOption;
import org.grayrabbit.cms.entity.VoteRecord;
import org.grayrabbit.cms.service.category.CategoryService;
import org.grayrabbit.cms.service.content.ContentService;
import org.grayrabbit.cms.service.dictionary.DictionaryService;
import org.grayrabbit.cms.service.plugins.VoteOptionService;
import org.grayrabbit.cms.service.plugins.VoteRecordService;
import org.grayrabbit.cms.service.plugins.VoteService;
import org.grayrabbit.cms.service.site.SiteService;
import org.grayrabbit.cms.util.FilePathSptUtil;
import org.grayrabbit.cms.util.IpUtil;
import org.grayrabbit.cms.vo.plugins.VoteBean;
import org.grayrabbit.cms.vo.plugins.VoteOptionBean;
import org.grayrabbit.cms.vo.plugins.VoteRecordBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;

/**
 * <b>VoteAction</b> Struts2 action�?





 * @version 1.0,创建时间:2011-05-16 16:08:35 
 */
@Controller
@Scope("prototype")
@Results( {
		@Result(name = "reload", type = "redirect", location = "vote-list"),
		@Result(name = "input", type = "dispatcher", location = "vote-input.jsp"),
		@Result(name = "success", type = "dispatcher", location = "vote-list.jsp")
		}
		)
public class VoteAction extends BaseAction<VoteBean> {
	private static final long serialVersionUID = -1L;
	public static final String RESULT = "result";
	//Service接口接入
	@Autowired
	@Qualifier("voteOptionServiceImpl")
	private VoteOptionService voteOptionService;
	
	@Autowired
	@Qualifier("voteRecordServiceImpl")
	private VoteRecordService voteRecordService;
	
	@Autowired
	@Qualifier("voteServiceImpl")
	private VoteService voteService;
	
	@Autowired
	@Qualifier("dictionaryServiceImpl")
	private DictionaryService dictionaryService;
	
	@Autowired
	@Qualifier("categoryServiceImpl")
	private CategoryService categoryService;
	
	@Autowired
	@Qualifier("siteServiceImpl")
	private SiteService siteService;
	
	@Autowired
	@Qualifier("contentServiceImpl")
	protected ContentService contentService;
	
	private List<Dictionary> dicvoteStatus;
	
	private List<Dictionary> dicvoteRepeatSubmit;
	
	private List<Dictionary> dicvoteCheckType;
	
	private List<Dictionary> dicInterviewee;
	
	private List<Dictionary> dicIscomment;
	
	private String upId;

	//VO对象，由前端页面数据组装
	private VoteBean vote;
	
	private VoteOptionBean voteOption;
	
	private VoteRecordBean voteRecordBean;
	
	//批量操作时使用的传入的ID
	private String ids;
	
	private String[] optArray;
	
	private String[] ordArray;
	
	private String[] optIdArray;
	
	private String orig;
	
	private String votename;
	
	private List<VoteOption> optList;
	
	private String siteId;
	
	private String arr = "";
	
	private Vote vot;

	int optNum = 0;
	
	private String ip;
	
	private List<Category> topCatList;
	
	private List<Vote> voList;
	
	protected Site site;
	
	protected String resource_url;
	
	protected String upload_url;

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}
	
	public String getUpload_url() {
		return upload_url;
	}

	public void setUpload_url(String upload_url) {
		this.upload_url = upload_url;
	}
	protected List<Content> rollAnnounceList;
	
	public List<Content> getRollAnnounceList() {
		return rollAnnounceList;
	}

	public void setRollAnnounceList(List<Content> rollAnnounceList) {
		this.rollAnnounceList = rollAnnounceList;
	}

	public Site getSite() {
		return site;
	}

	public void setSite(Site site) {
		this.site = site;
	}

	public String getResource_url() {
		return resource_url;
	}

	public void setResource_url(String resource_url) {
		this.resource_url = resource_url;
	}

	public List<Vote> getVoList() {
		return voList;
	}

	public void setVoList(List<Vote> voList) {
		this.voList = voList;
	}

	public List<Category> getTopCatList() {
		return topCatList;
	}

	public void setTopCatList(List<Category> topCatList) {
		this.topCatList = topCatList;
	}

	public int getOptNum() {
		return optNum;
	}

	public void setOptNum(int optNum) {
		this.optNum = optNum;
	}

	public Vote getVot() {
		return vot;
	}

	public void setVot(Vote vot) {
		this.vot = vot;
	}

	public String getArr() {
		return arr;
	}

	public void setArr(String arr) {
		this.arr = arr;
	}

	public void setSiteId(String siteId) {
		this.siteId = siteId;
	}

	public List<VoteOption> getOptList() {
		return optList;
	}

	public void setOptList(List<VoteOption> optList) {
		this.optList = optList;
	}

	public String getOrig() {
		return orig;
	}

	public void setOrig(String orig) {
		this.orig = orig;
	}
	public String[] getOptArray() {
		return optArray;
	}

	public void setOptArray(String[] optArray) {
		this.optArray = optArray;
	}

	public String[] getOrdArray() {
		return ordArray;
	}

	public void setOrdArray(String[] ordArray) {
		this.ordArray = ordArray;
	}
	
	public String[] getOptIdArray() {
		return optIdArray;
	}

	public void setOptIdArray(String[] optIdArray) {
		this.optIdArray = optIdArray;
	}
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<Dictionary> getDicvoteStatus() {
		return dicvoteStatus;
	}

	public void setDicvoteStatus(List<Dictionary> dicvoteStatus) {
		this.dicvoteStatus = dicvoteStatus;
	}
	
	public List<Dictionary> getDicvoteRepeatSubmit() {
		return dicvoteRepeatSubmit;
	}

	public void setDicvoteRepeatSubmit(List<Dictionary> dicvoteRepeatSubmit) {
		this.dicvoteRepeatSubmit = dicvoteRepeatSubmit;
	}

	public List<Dictionary> getDicvoteCheckType() {
		return dicvoteCheckType;
	}

	public void setDicvoteCheckType(List<Dictionary> dicvoteCheckType) {
		this.dicvoteCheckType = dicvoteCheckType;
	}
	public List<Dictionary> getDicIscomment() {
		return dicIscomment;
	}

	public void setDicIscomment(List<Dictionary> dicIscomment) {
		this.dicIscomment = dicIscomment;
	}

	public List<Dictionary> getDicInterviewee() {
		return dicInterviewee;
	}

	public void setDicInterviewee(List<Dictionary> dicInterviewee) {
		this.dicInterviewee = dicInterviewee;
	}
	
	
	
	/**return 前端页面数据组装VO对象,<b>VoteBean</b>*/
	public VoteBean getVote() {
		return vote;
	}

	public void setVote(VoteBean vote) {
		this.vote = vote;
	}
	
	public VoteOptionBean getVoteOption() {
		return voteOption;
	}

	public void setVoteOption(VoteOptionBean voteOption) {
		this.voteOption = voteOption;
	}
	//分页对象
	private Page<Vote> page = new Page<Vote>(MAX_PAGE_NUM,true);
	private Page<VoteRecord> page2 = new Page<VoteRecord>(MAX_PAGE_NUM,true);
	
	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<Vote> getPage() {
		return page;
	}
	//接收前端form,url提交参数组装成分页property
	public void setPage(Page<Vote> page) {
		this.page = page;
	}
	
	public Page<VoteRecord> getPage2() {
		return page2;
	}

	public void setPage2(Page<VoteRecord> page2) {
		this.page2 = page2;
	}

	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList(){
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.VOTESTATUS)!=null){
			dicvoteStatus = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.VOTESTATUS);
		}
		prepareModel();
	}
	
	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare(){
		if(vote==null){
			vote = new VoteBean();
		}
	}
	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法,
	 *  如：
	 *	<pre>
	 *		<code> name="user.userName"</code>
	 *	改写成：
	 *		<code> name="userName"</code>
	 *	action实例中，会自动组成user成员变量
	 *	</pre>
	 */
	public VoteBean getModel() {
		return vote;
	}
	
	@Override
	protected void prepareModel(){
		if(vote != null && StringUtils.isNotBlank(vote.getVoteId())){
			Vote item = voteService.getVote(vote.getVoteId());
			vote = (VoteBean)dozer.map(item,VoteBean.class);
			vote.setSite(item.getSite());
			optList = voteOptionService.findByVoteId(vote.getVoteId());
		}
	}
	
	/**
	 * 获取数据字典KEY值对应的NAME
	 * 
	 * @param key
	 * @param type
	 *            数据字典类型
	 * @return
	 */
	public String getName(String key,String code) {
		return dictionaryService.getDictionaryByDictionaryKey(key,code).getDictionaryValue();
	}
	
	/**
	 * 删除<b>Vote</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到vote-list.action
	 */
	@Override
	@Action(value="vote-delete",
			results={@Result(name="reload",type="redirectAction",params={"actionName","vote-list"})}
			)
	public String delete(){
		String[] idBatchs=getBatchIds();
		if(idBatchs != null){
			for(int i=0;i<idBatchs.length;i++){
				List<VoteOption> list = voteOptionService.findByVoteId(idBatchs[i]);
				String[] optIds = new String[list.size()];
				for(int j=0;j<list.size();j++){
					optIds[j] = list.get(j).getVoteOptionId();
					List<VoteRecord> reList = voteRecordService.findByOptId(list.get(j).getVoteOptionId());
					String [] recIds = new String[reList.size()];
					for(int k =0;k<reList.size();k++){
						recIds[k] = reList.get(k).getVoteRecordId();
					}
					voteRecordService.delete(recIds);
				}
				voteOptionService.delete(optIds);
			}
			voteService.delete(idBatchs);
			for(int i=0;i<idBatchs.length;i++){
				addLog(Module.PLUGIN.getValue(),OperAct.DEL.getValue(),idBatchs[i]);
			}
		}else{
			List<VoteOption> list = voteOptionService.findByVoteId(key[0]);
			String[] optIds = new String[list.size()];
			for(int i=0;i<list.size();i++){
				optIds[i] = list.get(i).getVoteOptionId();
				List<VoteRecord> reList = voteRecordService.findByOptId(list.get(i).getVoteOptionId());
				String [] recIds = new String[reList.size()];
				for(int k =0;k<reList.size();k++){
					recIds[k] = reList.get(k).getVoteRecordId();
				}
				voteRecordService.delete(recIds);
			}
			voteOptionService.delete(optIds);
			voteService.delete(key);
			addLog(Module.PLUGIN.getValue(),OperAct.DEL.getValue(),key[0]);
		}
		return RELOAD;
	}
	
	/**
	 * 批量操作，ID处理
	 * @return
	 */
	private String[] getBatchIds(){
		String[] idBatchs=null;
		if((null!=ids)&&(StringUtils.isNotBlank(ids))){
			if(ids.contains("on")){
				idBatchs=ids.substring(3).split("@");
			}else{
				idBatchs=ids.split("@");
			}
		}
		return idBatchs;
	}
	
	/**
	 * Vote的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到vote-list.jsp页面
	 */
	@Override
	@Action(value="vote-list")
	public String list(){
		Site currentSite = (Site)getSessionValue(ConstantWebApp.CURRENT_SITE);
		if(currentSite!=null && StringUtils.isNotBlank(currentSite.getSiteId())){
			vote.setSite(currentSite);
			if(StringUtils.isNotBlank(vote.getIsEnebleForPage())){
				vote.setVoteStatus(vote.getIsEnebleForPage().equals("true")?true:false);
			}
			voteService.list(page,vote);
			return SUCCESS;
		}else{
			return ERROR;
		}
		
	}
	/**
	 * Vote的新增与编辑的方法执行成功能
	 * 将映射到vote-input.jsp
	 */
	@Action(value="vote-input")
	public String input(){
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.VOTESTATUS)!=null){
			dicvoteStatus = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.VOTESTATUS);
		}
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.VOTEREPEATSUBMIT)!=null){
			dicvoteRepeatSubmit = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.VOTEREPEATSUBMIT);
		}
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.VOTECHECKTYPE)!=null){
			dicvoteCheckType = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.VOTECHECKTYPE);
		}
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.INTERVIEWEE)!=null){
			dicInterviewee = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.INTERVIEWEE);
		}
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_COMMENT)!=null){
			dicIscomment = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_COMMENT);
		}
		prepareModel();
		return INPUT;
	}
	
	/**
	 * save or update Vote的方法，将成员property vote转成Vote实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到vote-list.action
	 */
	@Override
	@Action(value="vote-save",
			results={@Result(name="reload",type="redirectAction",params={"actionName","vote-list"})}
		)
	public String save(){
		Vote item = null;
		Vote obj = null;
		if(StringUtils.isNotBlank(vote.getVoteId())){
			 item = voteService.getVote(vote.getVoteId());
			 obj = (Vote) dozer.map(vote, Vote.class);
			obj.setSite(item.getSite());
		}else{
			 obj = (Vote) dozer.map(vote, Vote.class);
		}
		if(!StringUtils.isNotBlank(obj.getVoteId())){
			obj.setVoteId(null);
		}
		if(!StringUtils.isNotBlank(obj.getVoteId())){
			if(!voteList().isEmpty() && voteList().get(0).getOrd() != null) {
				obj.setOrd(voteList().get(0).getOrd() + 1);
			}else {
				obj.setOrd(1);
			}
		}
		Admin admin = getCurrentAdmin();//从session获得登陆用户信息
		Site currentSite = (Site)getSessionValue(ConstantWebApp.CURRENT_SITE);
		if(StringUtils.isBlank(vote.getVoteId())){
			obj.setSite(currentSite);
			obj.setCreateTime(new Date());
			if(admin != null){
				obj.setCreateLoginName(admin.getLoginName());
				obj.setCreateRealName(admin.getRealName());
			}
		}else{
			obj.setUpdateTime(new Date());
			if(admin != null){
				obj.setUpdateLoginName(admin.getLoginName());
				obj.setUpdateRealName(admin.getRealName());
			}
		}
			voteService.save(obj);
			if(StringUtils.isBlank(vote.getVoteId())){
				addLog(Module.PLUGIN.getValue(),OperAct.ADD.getValue(),obj.getVoteId());
			}else{
				addLog(Module.PLUGIN.getValue(),OperAct.EDIT.getValue(),vote.getVoteId());
			}
		// 投票选项保存	
			String [] optionArr = null;
			if(StringUtils.isNotBlank(optArray[0])){
				optionArr = optArray[0].split(",");
			}
			String [] ordsArr = null;
			if(StringUtils.isNotBlank( ordArray[0])){
				ordsArr = ordArray[0].split(",");
			}
			String [] idArray = null;
			if(StringUtils.isNotBlank(optIdArray[0])){
				idArray = optIdArray[0].split(",");
			}
			if(StringUtils.isBlank(vote.getVoteId())){
				for(int i=0;i<optionArr.length;i++){
					VoteOption voptionobj = new VoteOption();
					if(StringUtils.isNotBlank(optionArr[i])){
						voptionobj.setVoteOptionTitle(optionArr[i]);
					}
					if(ordsArr != null && ordsArr.length >0){
						if(null!=ordsArr[i] && !("".equals(ordsArr[i]))){
							voptionobj.setOrd(Integer.parseInt(ordsArr[i]));
						}
					}
					voptionobj.setVote(new Vote(obj.getVoteId()));
					voteOptionService.save(voptionobj);
				}
			}else{
				if(idArray != null && optArray!= null && ordsArr!= null){
					for(int i=0;i<idArray.length;i++){
						VoteOption vo = voteOptionService.getVoteOption(idArray[i]);
						if(!vo.getVoteOptionTitle().equals(optionArr[i])){
							vo.setVoteOptionTitle(optionArr[i]);
						}
						vo.setOrd(Integer.parseInt(ordsArr[i]));
						voteOptionService.save(vo);
					}
					if(idArray.length < optionArr.length){
						for(int j=idArray.length;j<optionArr.length;j++){
							VoteOption voptionobj = new VoteOption();
							voptionobj.setVoteOptionTitle(optionArr[j]);
							if(ordsArr.length == optionArr.length){
								voptionobj.setOrd(Integer.parseInt(ordsArr[j]));
							}
							voptionobj.setVote(new Vote(obj.getVoteId()));
							voteOptionService.save(voptionobj);
						}
					}
				}
			}
		return RELOAD;
	}
	private List<Vote> voteList() {
		Site currentSite = (Site)getSessionValue(ConstantWebApp.CURRENT_SITE);
		List<Vote> list = voteService.voteList(currentSite.getSiteId());
		return list;
	}

	/**
	 * 禁用/启用 投票
	 * @return
	 */
	@Action(value="vote-disableorenable")
	public String disableOrEnable(){
		String text="";
		Vote obj=null;
		try {
			if(null!=vote&&StringUtils.isNotBlank(vote.getVoteId())){
				obj = voteService.getVote(vote.getVoteId());
				if(null!=obj.getVoteStatus()&&!obj.getVoteStatus()){
					obj.setVoteStatus(true);
					addLog(Module.PLUGIN.getValue(),OperAct.ENABLE.getValue(),vote.getVoteId());
				}else{
					addLog(Module.PLUGIN.getValue(),OperAct.DISABLE.getValue(),vote.getVoteId());
					obj.setVoteStatus(false);
				}
				voteService.save(obj);
			}
			text="ok";
		} catch (Exception e) {
			log.debug(e.getMessage());
			text="no";
			
		}
		return renderText(text);
	}
	
	/**
	 * 批量禁用
	 */
	@Action(value="vote-batchdisable")
	public String batchDisable(){
		String text="";
		try {
			String[] idBatchs=getBatchIds();
			voteService.disable(idBatchs);
			for(int i=0;i<idBatchs.length;i++){
				addLog(Module.PLUGIN.getValue(),OperAct.DISABLE.getValue(),idBatchs[i]);
			}
			text="ok";
		} catch (Exception e) {
			text="no";
			log.debug(e.getMessage());
		}
		return renderText(text);
	}
	/**
	 * 批量启用
	 */
	@Action(value="vote-batchenable")
	public String batchEnable(){
		String text="";
		try {
			String[] idBatchs=getBatchIds();
			voteService.enable(idBatchs);
			for(int i=0;i<idBatchs.length;i++){
				addLog(Module.PLUGIN.getValue(),OperAct.ENABLE.getValue(),idBatchs[i]);
			}
			text="ok";
		} catch (Exception e) {
			text="no";
			log.debug(text);
		}
		return renderText(text);
	}
	/**
	 * 唯一验证
	 * @return
	 */
	@Action(value="vote-checknameunique")
	public String  checkNameUnique(){
		String text="";
		try{
			Site currentSite = (Site)getSessionValue(ConstantWebApp.CURRENT_SITE);
			Boolean unique=voteService.checkNameUnique(vote.getVoteTitle().trim(),votename,currentSite.getSiteId());
			if(!unique){
				text="ok";
			}else{
				text="no";
			}
		}catch(Exception e){
			text="no";
		}
		return renderText(text);
	}
	
	/**
	 * 上移
	 * @return
	 */
	@Action(value="vote-upmove")
	public String upmove(){
		Vote obj = null;
		Vote upObj = null;
		if(vote != null && StringUtils.isNotBlank(vote.getVoteId())){
			obj = voteService.getVote(vote.getVoteId());
			upObj = voteService.getVote(upId);
			Integer cord = obj.getOrd();
			Integer uord = upObj.getOrd();
			obj.setOrd(uord);
			upObj.setOrd(cord);
			voteService.save(obj);
			voteService.save(upObj);
		}
		return null;
	}
	/**
	 * 下移
	 * @return
	 */
	@Action(value="vote-downmove")
	public String downmove(){
		Vote obj = null;
		Vote upObj = null;
		if(vote != null && StringUtils.isNotBlank(vote.getVoteId())){
			obj = voteService.getVote(vote.getVoteId());
			upObj = voteService.getVote(upId);
			Integer cord = obj.getOrd();
			Integer uord = upObj.getOrd();
			obj.setOrd(uord);
			upObj.setOrd(cord);
			voteService.save(obj);
			voteService.save(upObj);
		}
		return null;
	}

	/**
	 * 显示投票
	 * @return
	 */
	@Action(value="vote-showvote")
	public String showVote(){
		List<Vote> voteList = new ArrayList<Vote>();
		List<VoteOption> voteOptList = new ArrayList<VoteOption>();
		StringBuffer sb = new StringBuffer();
		
		voteList = voteService.findBySiteId(siteId);
		if(voteList.size()>0){
			voteOptList = voteOptionService.findByVoteId(voteList.get(0).getVoteId());
		}
		
		//获得多少人对此投票进行投票
		List<VoteRecord> optList = null;
		
		if(voteList.size()>0){
			optList = voteRecordService.findByVoteId(voteList.get(0).getVoteId());
			optNum = optList.size();
		}
		ip = IpUtil.getClientIp(getRequest());
		if(voteOptList.size()>0){
			sb.append("<input type=\"hidden\" id=\"ip\" value="+ip+">");
			sb.append("<input type=\"hidden\" id=\"voteId\" value="+voteList.get(0).getVoteId()+">");
			sb.append("<input type=\"hidden\" id=\"intervalTime\" value="+0.05+">");
			sb.append("已有 <span id=\"optNum\" class=\"cf00\">"+optNum+"</span> 人参与投票");
			sb.append("<h4 id="+voteList.get(0).getVoteId()+">"+voteList.get(0).getVoteTitle()+"</h4>");//得到投票主题标题
			sb.append("<div class=\"ul_12\">");
			//得到投票主题的选项
			 Boolean aa = new Boolean(false);
			 if(aa.equals(voteList.get(0).getVoteCheckType())){
				for(int i=0;i<voteOptList.size();i++){
						sb.append("<span><input type=\"radio\" onclick=\"getopt("+voteOptList.get(i).getVoteOptionId()+")\" name=\"voteopt\" value="+voteOptList.get(i).getVoteOptionId()+">"+voteOptList.get(i).getVoteOptionTitle()+"</span>");
				}
			 }else{
				 for(int i=0;i<voteOptList.size();i++){
					sb.append("<span><input type=\"checkbox\" onclick=\"getopt("+voteOptList.get(i).getVoteOptionId()+")\" name=\"voteopt\" value="+voteOptList.get(i).getVoteOptionId()+">"+voteOptList.get(i).getVoteOptionTitle()+"</span>");
				}
			 }
			sb.append("</div>");
		}else{
			sb.append("没有投票信息");
		}
		return renderHtml(sb.toString());
	}
	
	/**
	 * 查看投票结果
	 * @return
	 */
	@Action(value="vote-lookresult",results={@Result(name="success",location="/WEB-INF/content/plugins/voterecord-result.jsp")}) 
	public String lookResult(){
		
		showNotice();
		
		resource_url = FilePathSptUtil.STATIC_RESOURCES_URL;
		upload_url = FilePathSptUtil.UPLOAD_URL;
		site = siteService.getSite(siteId);
		//获得顶级栏目列表
		if(StringUtils.isNotBlank(siteId)){
			topCatList = categoryService.getTopCategory(siteId);
		}
		//获得投票主题列表
		if(StringUtils.isNotBlank(siteId)){
			voList = voteService.findBySiteId2(siteId,vote.getVoteId());
		}
		
		//投票结果数据
		if(StringUtils.isNotBlank(vote.getVoteId())){
			vot = voteService.getVote(vote.getVoteId());
			List<VoteRecord> optList = voteRecordService.findByVoteId(vote.getVoteId());
			optNum = optList.size();
			List<VoteOption> voteOptList = voteOptionService.findByVoteId(vote.getVoteId());
			for(int i=0;i<voteOptList.size();i++){
				List<VoteRecord> olist = voteRecordService.findByOptId(voteOptList.get(i).getVoteOptionId());
				arr = arr+voteOptList.get(i).getVoteOptionTitle()+"|"+olist.size()+"|";
			}
			arr = arr.substring(0, arr.length()-1);
		}
		return SUCCESS;
	}

	/**
	 * 后台查看投票结果
	 * @return
	 */
	@Action(value="vote-lookhouresult",results={@Result(name="result",location="/WEB-INF/content/plugins/vote-result.jsp")}) 
	public String lookResultHou(){
		//投票结果数据
				if(StringUtils.isNotBlank(vote.getVoteId())){
					vot = voteService.getVote(vote.getVoteId());
					List<VoteRecord> optList = voteRecordService.findByVoteId(vote.getVoteId());
					optNum = optList.size();
					List<VoteOption> voteOptList = voteOptionService.findByVoteId(vote.getVoteId());
					for(int i=0;i<voteOptList.size();i++){
						List<VoteRecord> olist = voteRecordService.findByOptId(voteOptList.get(i).getVoteOptionId());
						arr = arr+voteOptList.get(i).getVoteOptionTitle()+"|"+olist.size()+"|";
					}
					arr = arr.substring(0, arr.length()-1);
				}
				
				if(StringUtils.isNotBlank(vote.getVoteId())){
					voteRecordBean = new VoteRecordBean();
					voteRecordBean.setVote(new Vote(vote.getVoteId()));
					voteRecordService.list(page2, voteRecordBean);
				}
				return RESULT;
	}
	/**
	 * 根据投票项id获得投票项标题
	 * @param optionId
	 * @return
	 */
	public String getOptionName(String optionId){
		VoteOption vo = voteOptionService.getVoteOption(optionId);
		return vo.getVoteOptionTitle();
	}

	/**
	 * 获得投票总数
	 * @return
	 */
	@Action(value="vote-showvotenum")
	public String showVoteNum(){
		String text = "";
		List<Vote> voteList = new ArrayList<Vote>();
		@SuppressWarnings("unused")
		List<VoteOption> voteOptList = new ArrayList<VoteOption>();
		voteList = voteService.findBySiteId(siteId);
		if(voteList.size()>0){
			voteOptList = voteOptionService.findByVoteId(voteList.get(0).getVoteId());
		}
		//获得多少人对此投票进行投票
		List<VoteRecord> optList = null;
		if(voteList.size()>0){
			optList = voteRecordService.findByVoteId(voteList.get(0).getVoteId());
			optNum = optList.size();
			text  = optNum+"";
		}
		return renderText(text);
	}
	
	public void showNotice(){
		long[] limit = {0L,8L};
		String[] rollannounceIds = {"40288359328a66b201328b2b0edc001f"};
		rollAnnounceList = contentService.ftlFindAll(null,null,null,null, null, rollannounceIds, new String[]{}, null, null, null, null, null, limit, false, 10, 1);
	}
	 
	public String getSiteId(){
		Site currentSite = (Site)getSessionValue(ConstantWebApp.CURRENT_SITE);
		
		return currentSite.getSiteId();
	}
	
	public String getVotename() {
		return votename;
	}

	public void setVotename(String votename) {
		this.votename = votename;
	}

	public String getUpId() {
		return upId;
	}

	public void setUpId(String upId) {
		this.upId = upId;
	}
}